package com.iretailer.report.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iretailer.report.dto.PageResult;
import com.iretailer.report.dto.QueryOption;
import com.iretailer.report.mapper.FaceAlarmRecordMapper;
import com.iretailer.report.mapper.FaceImageMapper;
import com.iretailer.report.model.FaceAlarmMall;
import com.iretailer.report.model.FaceAlarmRecord;
import com.iretailer.report.model.FaceImage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

/**
 * @Auther: xjw
 * @Date: 2019/11/14 0014 17:18
 * @Description:
 */
@Service
public class FaceAlarmRecordService extends ServiceImpl<FaceAlarmRecordMapper, FaceAlarmRecord> {

    @Autowired
    private FaceAlarmRecordMapper faceAlarmRecordMapper;
    @Autowired
    private FaceImageMapper faceImageMapper;

    public PageResult<FaceAlarmRecord> gridFaceAlarmRecord(Long did, LocalDate date, Integer status, QueryOption queryOption) {
        Page<FaceAlarmRecord> page = new Page<>(1, Long.MAX_VALUE);
        QueryWrapper<FaceAlarmRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("did", did);
        queryWrapper.eq("date", date);
        if (status != null) {
            queryWrapper.eq("status", status);
        }
        queryOption.judge(page, queryWrapper);
        queryWrapper.orderByDesc("DateTime");
        faceAlarmRecordMapper.selectPage(page, queryWrapper);
        return new PageResult<>(page.getTotal(), page.getRecords());
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public boolean saveRecordAndImage(FaceAlarmRecord face) {
        save(face);
        faceImageMapper.insert(new FaceImage(face.getImageKey(), face.getImage()));
        return true;
    }
}
